package cn.csxw.openchina.ui.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import java.io.Serializable;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.csxw.openchina.R;
import cn.csxw.openchina.base.BaseActivity;
import cn.csxw.openchina.dagger2.component.DaggerRecentlyDetailActivityComponent;
import cn.csxw.openchina.dagger2.module.RecentlyDetailActivityModules;
import cn.csxw.openchina.dagger2.presenter.BasePresenter;
import cn.csxw.openchina.dagger2.presenter.RecentlyDetailActivityPresenter;
import cn.csxw.openchina.entity.ResultState;
import cn.csxw.openchina.entity.bean.EventBean;
import cn.csxw.openchina.entity.bean.PostBean;
import cn.csxw.openchina.utils.L;

/**
 * 活动详情数据的 获取
 */
public class RecentlyDetailActivity extends BaseActivity {
    @BindView(R.id.tv_recently_detail_webView)
    WebView mTvRecentlyDetailWebView;
    private EventBean mEventBean;
    @Inject
    RecentlyDetailActivityPresenter mActivityPresenter;

    @Override
    public int getLayoutRes() {
        return R.layout.activity_recently_detail;
    }

    @Override
    public void initView() {
        WebSettings settings = mTvRecentlyDetailWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setBuiltInZoomControls(true);//支持内容压缩数据
        settings.setAllowFileAccess(true);//允许访问文件数据
        mTvRecentlyDetailWebView.setWebViewClient(mWebViewClient);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mTvRecentlyDetailWebView.canGoBack()) {
            //goBack()表示返回WebView的上一页面  否则就直接退出到刚开始的界面
            mTvRecentlyDetailWebView.goBack();
            return true;
        }
        return false;
    }

    //WebViewClient网络数据的监听
    WebViewClient mWebViewClient = new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            L.d("活动界面访问数据成功");
        }

        @Override
        public void onReceivedError(WebView view,
                                    WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            L.d("活动界面访问数据失败");
        }

    };


    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        //获取RecentlyDetailAdapter传递过来的数据
        mEventBean = (EventBean) getIntent().getSerializableExtra("RECENTLY");
        getDetailData();
    }

    //获取数据
    private void getDetailData() {
        DaggerRecentlyDetailActivityComponent.builder()
                .recentlyDetailActivityModules(new RecentlyDetailActivityModules(this))
                .build()
                .inject(this);
        mActivityPresenter.setOnResultStateListener(mOnResultStateListener);
        String postBeanId = "";
        if (mEventBean != null) {
            postBeanId = mEventBean.id;
            mActivityPresenter.callNetWork(postBeanId);
        }
    }

    BasePresenter.OnResultStateListener mOnResultStateListener =
            new BasePresenter.OnResultStateListener() {
                @Override
                public void onStateError(Throwable t, ResultState errorState) {
                    L.d("活动详情获取失败在Activity中");
                }

                @Override
                public void onStateSuccess(ResultState successState) {
                    L.d("活动详情获取成功在Activity中");

                }

                @Override
                public void onStateEmpty(ResultState emptyState) {

                }
            };

    @Override
    public void onClick(View v, int id) {

    }

    @BindView(R.id.tv_recently_detail_title)
    TextView mTvRecentlyDetailTitle;
    @BindView(R.id.tv_recently_detail_start_time)
    TextView mTvRecentlyDetailStartTime;
    @BindView(R.id.tv_recently_detail_end_time)
    TextView mTvRecentlyDetailEndTime;
    @BindView(R.id.tv_recently_detail_address)
    TextView mTvRecentlyDetailAddress;
    @BindView(R.id.tv_recently_detail_)
    TextView mTvRecentlyDetail;

    //设置数据
    public void doNetWork(PostBean post) {
        mTvRecentlyDetailTitle.setText(post.title);
        mTvRecentlyDetailStartTime.setText(post.event.startTime);
        mTvRecentlyDetailEndTime.setText(post.event.endTime);
        mTvRecentlyDetailAddress.setText(post.event.spot);//地址
        mTvRecentlyDetailWebView.loadUrl(post.url);//设置webView地址
    }
}
